package com.isp.org.server.application.facade;

import com.isp.common.model.user.GroupDTO;
import com.isp.common.model.vo.Result;
import com.isp.org.server.domain.entity.GroupEntity;
import com.isp.org.server.domain.service.GroupService;
import com.isp.org.server.interfaces.model.group.mapper.GroupMapStruct;
import com.isp.org.server.interfaces.model.group.request.CreateGroupRequest;
import com.isp.org.server.interfaces.model.group.request.UpdateGroupRequest;
import com.isp.org.server.interfaces.model.group.response.GroupDetailResponse;
import com.isp.org.server.interfaces.model.group.response.GroupListResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/3/12 14:44
 */
@Service
@RequiredArgsConstructor
public class GroupFacade {

    private final GroupService groupService;

    public Boolean add(CreateGroupRequest request) {
        return groupService.add(GroupMapStruct.INSTANCE.createToEntity(request));
    }

    public Boolean delete(Long id) {
        return groupService.delete(id);
    }

    public Boolean update(Long id, UpdateGroupRequest request) {
        GroupEntity groupEntity = GroupMapStruct.INSTANCE.updateToEntity(request);
        groupEntity.setId(id);
        return groupService.update(groupEntity);
    }

    public GroupDetailResponse get(Long id) {
        GroupEntity groupEntity = groupService.get(id);
        return GroupMapStruct.INSTANCE.entityToDetail(groupEntity);
    }

    public Result<List<GroupListResponse>> query(String name) {
        List<GroupEntity> groupEntities = groupService.query(name);
        List<GroupListResponse> responseList = GroupMapStruct.INSTANCE.entitiesToLists(groupEntities);
        return Result.success(responseList);
    }

    public Result<List<GroupListResponse>> queryByUserName(String username) {
        List<GroupEntity> groupEntities = groupService.query(username);
        List<GroupListResponse> responseList = GroupMapStruct.INSTANCE.entitiesToLists(groupEntities);
        return Result.success(responseList);
    }

    public Result<List<GroupListResponse>> queryByParentId(Long parentId) {
        List<GroupEntity> groupEntities = groupService.queryByParentId(parentId);
        List<GroupListResponse> responseList = GroupMapStruct.INSTANCE.entitiesToLists(groupEntities);
        return Result.success(responseList);
    }

    public List<GroupDTO> selectByIds(Set<String> groupIds) {
        List<GroupEntity> groupEntities = groupService.selectByIds(groupIds);
        return GroupMapStruct.INSTANCE.entitiesToDtoList(groupEntities);
    }
}
